
var nbQuestion =1;
var nbReponse =3;
var idQuestion =1;
var idReponse1 = 1;
var idReponse2 = 2;
var idReponse3 = 3;

function $choixActivite($idPoint){
   
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({
        'width':maskWidth,
        'height':maskHeight
    });

    //Effet de transition
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow",0.8);

    //Recuperation des dimensions de l'ecran'
    var winH = $(window).height();
    var winW = $(window).width();

    //Placement de la pop-up au centre
    $('#typeA').css('top',  50);
    $('#typeA').css('left', winW/2-$('#typeA').width()/2);
    $('#typeA').fadeIn(2000);
   
    $('body').css('z-index',-1);

    $("#typeQuizz").click(function(){
        $("#typeA").hide();
        $("#mask").hide();
        $ajouterQuizz($idPoint);
    });


    $("#typeCF").click(function(){
        $("#typeA").hide();
        $("#mask").hide();
        $ajouterCF($idPoint);
    });

    $("#typeTL").click(function(){
        $("#typeA").hide();
        $("#mask").hide();
        $ajouterTL($idPoint);
    });
}


function $ajouterQuizz($idPoint){

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({
        'width':maskWidth,
        'height':maskHeight
    });

    //Effet de transition
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow",0.8);

    //Recuperation des dimensions de l'ecran'
    var winH = $(window).height();
    var winW = $(window).width();

    //Placement de la pop-up au centre
    $('#gestionActivite').css('top',  50);
    $('#gestionActivite').css('left', winW/2-$('#gestionActivite').width()/2);
    $('#gestionActivite').fadeIn(2000);

    $('body').css('z-index',-1);

    $("#addQ").click(function(){
        nbQuestion +=1;
        nbReponse +=3;
        idQuestion +=1;

        idReponse1=idReponse1+3;
        idReponse2=idReponse2+3;
        idReponse3=idReponse3+3;

        //Ajouter un nouveau formulaire pour proposer une nouvelle question
        $("#contenu").append(
            "<fieldset id='div"+idQuestion+"'>"+
            "<div class='div' id='div"+idQuestion+"'>"+
            "<div class='div'>"+
            " <label style='width:10px;'>Question</label><br /><br />"+
            "<input type='text' name='question' id='question"+idQuestion+"' value='' style='width:200px' required='required'/>"+
            "<input type='file' name='imageQ"+idQuestion+"' id='imageQ"+idQuestion+"' value='' onChange='getLien();'/>"+
            "<input type='checkbox' id='checkQ"+idQuestion+"' name='"+idQuestion+"' value='' />"+
            "</div>"+
            "<br />"+
            "<div class='div'>"+
            "<label style='width:10px;'>Propositions</label><br /><br />"+
            "<label style='width:10px;'>"+idReponse1+"-</label>"+
            "<input type='text' name='reponse' id='reponse"+idReponse1+"' value='' style='width:200px' required='required'/>"+
            "<input type='file' id='imgR"+idReponse1+"' value='' onChange='getLien();'/>"+
            "<input type='checkbox' id='checkR"+idReponse1+"' value='' />"+
            "</div>"+
            "<br />"+
            "<div class='div'>"+
            " <label style='width:10px;'>"+idReponse2+"-</label>"+
            "<input type='text' name='reponse' id='reponse"+idReponse2+"' value='' style='width:200px' required='required'/>"+
            "<input type='file' id='imgR"+idReponse2+"' value='' onChange='getLien();'/>"+
            " <input type='checkbox' id='checkR"+idReponse2+"' value='' />"+
            "</div><br />"+
            "<div class='div'>"+
            "<label style='width:10px;'>"+idReponse3+"-</label>"+
            "<input type='text' name='reponse' id='reponse"+idReponse3+"' value='' style='width:200px' required='required'/>"+
            "<input type='file' id='imgR"+idReponse3+"' value='' onChange='getLien();'/>"+
            "<input type='checkbox' id='checkR"+idReponse3+"' value='' />"+
            "</div>"+
            "</div></fieldset>");

    });


    $("#gestionA").submit(function(){

        var i;
        var j;
        var arrayQ = new Array();
        var arrayR = new Array();
        var arrayIMGR = new Array();
        var arrayIMGQ = new Array();
        var arrayCheck = new Array();
        var type = "quizz";

        //on recupere le nom du quizz
        var nomQuizz = $("#nomQ").val();


        //on recupere les questions et les liens img
        for(i = 1; i<=nbQuestion;i++){
            arrayQ[0] = "null";
            arrayQ[i] = $("#question"+i).val();
            arrayIMGQ[0] = "null";
            arrayIMGQ[i] = $("#imageQ"+i).val();
        }

        //on recupere les reponses et les liens img
        for(j = 1; j<=nbReponse;j++){
            arrayR[0] = "null";
            arrayIMGR[0] = "null"
            
            arrayR[j] = $("#reponse"+j).val();
            arrayIMGR[j] = $("#imgR"+j).val();
        }

        for(var z=1;z<=idReponse3;z++){
            arrayCheck[0] = "null";
            
            var check = $('#checkR'+z).is(':checked');
            arrayCheck[z] = check;
        }

        //verifier que au moins une bonne réponse est indique par groupe de reponse
        $('input[id$=submitQuizz]').click(function(e) {
            var checked = $('input:checkbox:checked').length;
            if (checked == 0) {
                alert('Vous devez au moins choisir une bonne réponse par question');
                e.preventDefault();
            }
        });

        var tableauQ = JSON.stringify(arrayQ);
        var tableauR = JSON.stringify(arrayR);
        var tableauImgQ = JSON.stringify(arrayIMGQ);
        var tableauImgR = JSON.stringify(arrayIMGR);
        var tableauC = JSON.stringify(arrayCheck);

        $.ajax({
            type: 'POST',
            url: 'gestionActivite.php',
            data:"action=ajouterQuizz&idPoint="+$idPoint+"&nomQ="+nomQuizz+"&nbQuestion="+nbQuestion+"&nbReponse="+nbReponse+"&question="+tableauQ+"&imgQ="+tableauImgQ+"&reponse="+tableauR+"&imgR="+tableauImgR+"&reponseC="+tableauC+"&type="+type,
            dataType:'json',
            success: function(data, textStatus, jqXHR) {
            //alert("test");
            },
            error: function(jqXHR, textStatus, errorThrown) {
            }
        });

        return false;
    });

    $("#supprQ").click(function(){
        $("input:checkbox:checked").each(function(){
            var id = $(this).parent().parent().attr("id");
            $('#'+id).remove();
        });
    });


}


function $ajouterTL($idPoint){
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({
        'width':maskWidth,
        'height':maskHeight
    });

    //Effet de transition
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow",0.8);

    //Recuperation des dimensions de l'ecran'
    var winH = $(window).height();
    var winW = $(window).width();

    //Placement de la pop-up au centre
    $('#temps_limite').css('top',  50);
    $('#temps_limite').css('left', winW/2-$('#temps_limite').width()/2);
    $('#temps_limite').fadeIn(2000);

    $('body').css('z-index',-1);

    $("#TL").submit(function(){

        var type = "temps_limite";

        //récupération de l'arc
        var arc = $("#choixArc :selected").text();

        //recuperation de la durée
        var duree = $("#minute").val();

        $.ajax({
            type: 'POST',
            url: 'gestionActivite.php',
            data:"action=ajouterTempsL&idPoint="+$idPoint+"&arc="+arc+"&duree="+duree+"&type="+type,
            success: function(data, textStatus, jqXHR) {
                alert("Temps Limité crée");
            },
            error: function(jqXHR, textStatus, errorThrown) {
            }
        });
        return false;
    });
 

}

function $ajouterCF($idPoint){
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({
        'width':maskWidth,
        'height':maskHeight
    });

    //Effet de transition
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow",0.8);

    //Recuperation des dimensions de l'ecran'
    var winH = $(window).height();
    var winW = $(window).width();

    //Placement de la pop-up au centre
    $('#chaud_froid').css('top',  50);
    $('#chaud_froid').css('left', winW/2-$('#chaud_froid').width()/2);
    $('#chaud_froid').fadeIn(2000);

    $('body').css('z-index',-1);


    $("#CF").submit(function(){

        var type = "chaud_froid";

        //récupération de l'arc
        var arc = $("#choixArcCF :selected").text();

        //recuperation de la durée
        var precision = $("#precision").val();

        //alert(type +" "+arc+ " "+ precision)
        $.ajax({
            type: 'POST',
            url: 'gestionActivite.php',
            data:"action=ajouterCF&idPoint="+$idPoint+"&arc="+arc+"&precision="+precision+"&type="+type,
            success: function(data, textStatus, jqXHR) {
                alert("Chaud Froid crée");
            },
            error: function(xhr, textStatus, thrownError) {
                alert(xhr.status);
                alert(xhr.statusText);
                alert(thrownError);
            }
        });
        return false;

    });

}




// Fonction stoppant toute évènement natif et leur propagation
function drop(evt){
    do_nothing(evt);
		
    var files = evt.dataTransfer.files;
	
    // On vÃ©rifie que des fichiers ont bien Ã©tÃ© dÃ©posÃ©s
    if(files.length>0){
        for(var i in files){
            // Si c'est bien un fichier
            if(files[i].size!=undefined) {
				
                var fic=files[i];
				
                // On ajoute un listener progress sur l'objet xhr de jQuery
                xhr = jQuery.ajaxSettings.xhr();
                if(xhr.upload){
                    xhr.upload.addEventListener('progress', function (e) {
                        console.log(e);
                        update_progress(e,fic);
                    },false);
                }
                provider=function(){
                    return xhr;
                };
				
                // On construit notre objet FormData
                var fd=new FormData;
                fd.append('fic',fic);
				
                // Requete ajax pour envoyer le fichier
                $.ajax({
                    url:'save_fic.php',
                    type: 'POST',
                    data: fd,
                    xhr:provider,
                    processData:false,
                    contentType:false,
                    complete:function(data){
                        $('#'+data.responseText+' .percent').css('width', '100%');
                        $('#'+data.responseText+' .percent').html('100%');
                    },
                    success:function (results){
                    }
                });
						
				
                // On prÃ©pare la barre de progression au dÃ©marrage
                var id_tmp=fic.size;
                $('#output').after('<div class="progress_bar loading" id="'+id_tmp+'"><div class="percent">0%</div></div>');
                $('#output').addClass('output_on');
				
                // On ajoute notre fichier Ã  la liste
                $('#output-listing').append('<li>'+files[i].name+'</li>');
				
            }
        }
    }

}

// Fonction stoppant toute Ã©vÃ¨nement natif et leur propagation
function do_nothing(evt){
    evt.stopPropagation();
    evt.preventDefault();
}

// Mise Ã  jour de la barre de progression
function update_progress(evt,fic) {
	
    var id_tmp=fic.size;
	
    if (evt.lengthComputable) {
        var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
        if (percentLoaded <= 100) {
            $('#'+id_tmp+' .percent').css('width', percentLoaded + '%');
            $('#'+id_tmp+' .percent').html(percentLoaded + '%');
        }
    }
}

function getLien(){

    var i;
    var j;

    //alert(nbQuestion +"----------" + nbReponse);


    for(i = 1; i<=nbQuestion;i++){
        var lien = document.getElementById("imageQ"+i).value;
        document.getElementById("imgQ"+i).src = "./imagesUser/" + lien;
    }

    for(j=1; j<=nbReponse; j++){
        var lienR = document.getElementById("imgR"+j).value;
        document.getElementById("imageR"+j).src = "./imagesUser/" + lienR;
    }

}


